package com.xxkj.drugtraceability.ui.main

import android.content.Intent
import android.util.Base64
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.GridLayout
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xxhis.refactor.ext.dp2px
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.RoutBean
import com.xxkj.drugtraceability.databinding.ItemContentBinding
import com.xxkj.drugtraceability.databinding.ItemLayoutBinding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 9:45
 * description:
 */
class MainAdapter:BaseQuickAdapter<RoutBean, BaseDataBindingHolder<ItemLayoutBinding>>(R.layout.item_layout) {
    override fun convert(holder: BaseDataBindingHolder<ItemLayoutBinding>, item: RoutBean) {
        holder.dataBinding?.apply {
            tvTitle.text = item.meta.title.substring("PDA-".length)
            gridLayout.removeAllViews()
            item.children.forEach { child->
               val view =  LayoutInflater.from(context).inflate(R.layout.item_content,null)
                view.clickN {
                    //点击跳转到对应页面
                    try {
                        var intent = Intent()
                        intent.setClassName(view.context.packageName,child.path)
                        context.startActivity(intent)
                    } catch (e: Exception) {
                    }
                }
               val childView =  DataBindingUtil.bind<ItemContentBinding>(view)
                childView?.apply {
                    tvName.text =  child.meta.title
                    if (child.meta.menuIcon.isNotEmpty()){
                        val base64Image = child.meta.menuIcon.substringAfter(",")  // 去掉前缀
                        val imageByteArray = Base64.decode(base64Image, Base64.DEFAULT)
                        Glide.with(context)
                            .asBitmap()
                            .load(imageByteArray)
                            .override(context.dp2px(100), context.dp2px(80))  // 设置图片的宽度和高度（例如 200x200）
                            .into(iv)
                    }
                }
                // 创建布局参数
                if (item.children.size>1)
                {
                    val layoutParams = GridLayout.LayoutParams().apply {
                        width = 0  // 让按钮铺满列
                        height = GridLayout.LayoutParams.WRAP_CONTENT
                        columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)  // 占满列
                        if (getItemPosition(item)==data.size-1){
                            setMargins(0, 0, context.dp2px(10), context.dp2px(20))
                        }else{
                            setMargins(0, 0, context.dp2px(10), 0)
                        }
                    }
                    gridLayout.addView(view, layoutParams)
                }else{
                    val layoutParams = GridLayout.LayoutParams().apply {
                        width = 0 // 让按钮铺满列
                        height = GridLayout.LayoutParams.WRAP_CONTENT
                        columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)  // 占满列
                        if (getItemPosition(item)==data.size-1){
                            setMargins(0, 0, context.dp2px(10), context.dp2px(20))
                        }else{
                            setMargins(0, 0, context.dp2px(10), 0)
                        }
                    }
                    gridLayout.addView(view,layoutParams)
                    val viewEmpty =  LayoutInflater.from(context).inflate(R.layout.item_empty,null)
                    val layoutParamsEmpty = GridLayout.LayoutParams().apply {
                        width = 0 // 让按钮铺满列
                        height = GridLayout.LayoutParams.WRAP_CONTENT
                        columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)  // 占满列
                        if (getItemPosition(item)==data.size-1){
                            setMargins(0, 0, context.dp2px(10), context.dp2px(20))
                        }else{
                            setMargins(0, 0, context.dp2px(10), 0)
                        }
                    }
                    gridLayout.addView(viewEmpty,layoutParamsEmpty)
                }
            }
        }
    }

}